#import "SEGTabBarView.h"

@implementation SEGTabBarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = [UIScreen mainScreen].bounds.size.width;
        frame.size.height = kTabBarViewHeight;
    }
    self = [super initWithFrame:frame];
    [self addSubview:self.backgroundScrollView];
    self.selectIndex = 0;
    self.backgroundColor = [UIColor clearColor];
    self.backgroundScrollView.backgroundColor = [UIColor clearColor];
    return self;
}
- (void)setWithLayout:(SEGTabBarViewLayout *)layout {
    _layout = layout;
    [self setupUI];
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
    [super traitCollectionDidChange:previousTraitCollection];
    [_layout.data enumerateObjectsUsingBlock:^(SEGMenuListModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SEGEnumTabBarItemViewType type = SEGEnumTabBarItemViewType_NO;
        if ([obj.action isEqualToString:@"TAB_DOOR"]) {
            if ([obj.widgetType integerValue] == SEGEnumShowTypeTabNormal) {
                type = SEGEnumTabBarItemViewType_OPENDOOR;
            }
            if ([obj.widgetType integerValue] == SEGEnumShowTypeTabTop) {
                type = SEGEnumTabBarItemViewType_TOPOPENDOOR;
            }
            if ([obj.widgetType integerValue] == SEGEnumShowTypeTabPic) {
                type = SEGEnumTabBarItemViewType_PIC;
            }
        }
        
        for (SEGTabBarItemView *object in self.backgroundScrollView.subviews) {
            if ([object isKindOfClass:[SEGTabBarItemView class]]){
                if (object.tag == idx) {
                    [object setWithLayout:[[SEGTabBarItemViewLayout alloc]initWithData:obj type:type]];
                }
            }
        }
    
    }];
    
    //门禁特殊处理,用于防止点击开门tab，上一次的tab颜色变成normal
    BOOL isDoorWidget = NO;
    for (SEGTabBarItemView *object in self.backgroundScrollView.subviews) {
        if ([object isKindOfClass:[SEGTabBarItemView class]]) {
            if(object.tag == self.selectIndex && [object.layout.data.action isEqualToString:@"TAB_DOOR"]) {
                isDoorWidget = YES;
            }
        }
    }
    if (isDoorWidget) return;

    for (SEGTabBarItemView *object in self.backgroundScrollView.subviews) {
        if ([object isKindOfClass:[SEGTabBarItemView class]]){
            [object setSelected: (object.tag == _selectIndex)? YES :NO];
            [object segFreshImageShow: (object.tag == _selectIndex)? YES :NO];
        }
    }
    
    
}
- (void)layoutSubviews {
    [super layoutSubviews];
}
- (void)segLayoutSubviews {
    self.backgroundScrollView.frame = self.bounds;
    for (SEGTabBarItemView *object in self.backgroundScrollView.subviews) {
        if([object isKindOfClass:[SEGTabBarItemView class]]){
            object.y = CGRectGetHeight(self.backgroundScrollView.frame) - object.layout.totalHeight;
            if (object.layout.type == SEGEnumTabBarItemViewType_OPENDOOR) {
                [object setupNormalOpenDoor];
            }else if (object.layout.type == SEGEnumTabBarItemViewType_TOPOPENDOOR) { //凸起的开门
                [object setupTopDoor];
            }else if (object.layout.type == SEGEnumTabBarItemViewType_PIC) { //凸起的开门
                [object setupOpenDoor];
            }else{
                [object setupUI];
            }
        }
    }
}
//界面布局
- (void)setupUI {
    self.backgroundScrollView.frame = self.bounds;
    self.backgroundScrollView.clipsToBounds = NO;
    self.backgroundScrollView.layer.masksToBounds = NO;
    [self.backgroundScrollView removeAllSubviews];
    CGFloat W = LYScreenWidth() / (CGFloat)_layout.data.count;
    
    __weak typeof(self) _self = self;
    [_layout.data enumerateObjectsUsingBlock:^(SEGMenuListModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        CGRect rect = CGRectMake(W * idx, 0, W, self.bounds.size.height);
        
        SEGEnumTabBarItemViewType type = SEGEnumTabBarItemViewType_NO;
        if ([obj.action isEqualToString:@"TAB_DOOR"]) {
            if ([obj.widgetType integerValue] == SEGEnumShowTypeTabNormal) {
                type = SEGEnumTabBarItemViewType_OPENDOOR;
            }
            if ([obj.widgetType integerValue] == SEGEnumShowTypeTabTop) {
                type = SEGEnumTabBarItemViewType_TOPOPENDOOR;
            }
            if ([obj.widgetType integerValue] == SEGEnumShowTypeTabPic) {
                type = SEGEnumTabBarItemViewType_PIC;
            }
        }
        
        SEGTabBarItemView *view = [[SEGTabBarItemView alloc]initWithFrame:rect];
        [view setWithLayout:[[SEGTabBarItemViewLayout alloc]initWithData:obj type:type]];
        view.tag = idx;
       
        [_self.backgroundScrollView addSubview:view];
        view.y = CGRectGetHeight(_self.backgroundScrollView.frame) - view.layout.totalHeight;
        
        _self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(UITapGestureRecognizer*  _Nonnull sender) {
                _self.selectIndex = sender.view.tag;
        }];
        [view addGestureRecognizer:tap];
    }];
}

#pragma mark - setter
- (void)setSelectIndex:(NSInteger)selectIndex {
    //门禁特殊处理,用于防止点击开门tab，上一次的tab颜色变成normal
    for (SEGTabBarItemView *object in self.backgroundScrollView.subviews) {
        if ([object isKindOfClass:[SEGTabBarItemView class]]) {
            if(object.tag == selectIndex && ![object.layout.data.action isEqualToString:@"TAB_DOOR"]) {
                _selectIndex = selectIndex;
            }
        }
    }
    
    for (SEGTabBarItemView *object in self.backgroundScrollView.subviews) {
        if ([object isKindOfClass:[SEGTabBarItemView class]]){
            [object setSelected: (object.tag == _selectIndex)? YES :NO];
            // 让代理来处理切换viewController的操作
            if (object.tag == selectIndex) {
                if ([self.delegate respondsToSelector:@selector(segTabBarView:didSelectItemAtIndex:data:)]) {
                    [self.delegate segTabBarView:self didSelectItemAtIndex:selectIndex data:object.layout.data];
                }
            }
        }
    }
}

#pragma mark - Getter
- (UIScrollView *)backgroundScrollView {
    if (!_backgroundScrollView) {
        _backgroundScrollView = [[UIScrollView alloc]init];
        _backgroundScrollView.frame = self.frame;
        _backgroundScrollView.contentSize = CGSizeMake(0, 0);
    }
    return _backgroundScrollView;
}

@end

